package com.moyq5.spring.boot.app.register.server;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.core.MessageProducer;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.stereotype.Component;

/**
 * 监听任务消息
 * @author Moyq5
 * @date 2019年3月1日
 */
@Component("appInfoServerReceiver")
public class AppInfoReceiver {
	
	private static final String CLIENT_ID = AppInfoReceiver.class.getSimpleName() + String.valueOf(Math.round(Math.random() * 1e15));
	private static final String TOPIC = "app/info/from/app";
	@Autowired
	private MqttPahoClientFactory clientFactory;
	@Autowired
	private AppInfoHandler appInfoServerHandler;
    @Bean
	public MessageProducer appInfoServerProducer() {
    	MqttPahoMessageDrivenChannelAdapter adapter = 
				new MqttPahoMessageDrivenChannelAdapter(CLIENT_ID, clientFactory, TOPIC);
		adapter.setCompletionTimeout(5000);
		adapter.setConverter(new DefaultPahoMessageConverter());
		adapter.setQos(0);
		DirectChannel channel = new DirectChannel();
		channel.subscribe(appInfoServerHandler);
		adapter.setOutputChannel(channel);
		return adapter;
	}

}
